package com.sucisoft.base.ui.base;

import android.Manifest;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.sucisoft.base.Helper;
import com.sucisoft.base.R;
import com.sucisoft.base.config.AppConfig;
import com.sucisoft.base.tools.StatusBarCompat;
import com.sucisoft.base.tools.XActivityStack;
import com.sucisoft.base.tools.XToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * 2033152950
 * Created by zf on 2018/1/6.
 */

public abstract class BaseActivity extends AppCompatActivity {
    protected SharedPreferences SP;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(mLayoutView());
        ButterKnife.bind(this);
        onPermissions();
        setPermission();
        StatusBarCompat.compat(this, Color.parseColor(getColorString()));
        XActivityStack.getInstance().addActivity(this);
        SP = getSharedPreferences(AppConfig.SAVE_CONFIG_FILE, Context.MODE_PRIVATE);
        init();
    }

    @LayoutRes
    protected abstract int mLayoutView();

    protected abstract void init();

    protected String ColorString = null;

    public String getColorString() {
        return ColorString == null ? getString(R.string.parse_color) : ColorString;
    }

    public void setColorString(String colorString) {
        ColorString = colorString;
    }

    private static final int PERMISSION_OK = 99;
    protected List<String> permissions;

    protected void onPermissions() {
        permissions = new ArrayList<>();
        permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    public void setPermission() {
        if (Build.VERSION.SDK_INT > 22) {
            for (int i = 0; i < permissions.size(); i++) {
                if (ContextCompat.checkSelfPermission(this, permissions.get(i))
                        == PackageManager.PERMISSION_GRANTED) {
                    permissions.remove(permissions.get(i));
                }
            }
            if (permissions.size() > 0) {
                ActivityCompat.requestPermissions(this,
                        permissions.toArray(new String[permissions.size()]),
                        PERMISSION_OK);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_OK:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    onResultPermissions(requestCode);
                } else {
                    XToast.error(onPermissionToastStr()).show();
                }
                break;
            default:
                break;
        }
    }

    protected void onResultPermissions(int requestCode) {
    }

    protected String onPermissionToastStr() {
        return "请手动打开权限";
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Helper.obtian().cancel(this);
        XActivityStack.getInstance().finishActivity();
    }

}
